package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.user.entity.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.atguigu.gmall.user.vo.LoginSuccessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户")
@RestController
@RequestMapping("/api/user")  //  http://api.gmall.com/api/user/passport/login
public class LoginController {

    @Autowired
    UserInfoService userInfoService;

    //登录
    @ApiOperation("用户登录")
    @PostMapping("/passport/login")
    public Result login(@RequestBody UserInfo userInfo){
        //给前端返回 json {token:"",userId:"",nickName:""}
        LoginSuccessVo loginSuccessVo = userInfoService.login(userInfo);
        return Result.ok(loginSuccessVo);
    }

    //  http://api.gmall.com/api/user/passport/logout
    //退出，删除redis的数据
    @ApiOperation("用户退出")
    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader("token") String token){
        userInfoService.logout(token);
        return Result.ok();
    }
}
